﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace reader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        string sDir = System.Environment.CurrentDirectory;
        
        private void button1_Click(object sender, EventArgs e)
        {



           
//                string sourceDir=@"C:\\Users\\Hideya\\Desktop\\a-TROCIAcheckpoint\\";    
            string sourceDir = @""; // @"..\\..";    

                string kmaxS;
                int kmin = Convert.ToInt32(textBox3.Text);
                int kmax = Convert.ToInt32(textBox2.Text);
                int kwidth = Convert.ToInt32(textBox4.Text);
                int center = Convert.ToInt32(textBox1.Text); ;
                
                progressBar1.Minimum = 0;
                progressBar1.Maximum = kmax;
                for (int k = kmin; k <= kmax; k++)
                {
                    progressBar1.Value = k;
                    kmaxS = k.ToString();
                    if (kmaxS.Length == 1) { kmaxS = "000" + kmaxS; }
                    if (kmaxS.Length == 2) { kmaxS = "00" + kmaxS; }
                    if (kmaxS.Length == 3) { kmaxS = "0" + kmaxS; }
                    using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text+kmaxS+".dd2"))
                    {
                        string Efreq;        
                        String buffer;
                        String lineout;
                        string Exfreq = textBox1.Text;
                        bool q1 = true;
                        bool q2 = false;
                        bool q3 = true;
                        int q4 = 0;
                        int wlength;
                        int clength;
                        while (sr.Peek() >= 0)
                        {
                            buffer = sr.ReadLine();
                            if (buffer == "****")
                            {
                                q1 = false;
                            }

                            
                            string[] words = buffer.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
//                            string[] words = buffer.Split(' ');

                            char[] chars = buffer.ToCharArray();

                            wlength = words.Length;
                            clength = chars.Length;
                            //does line contain f?
                            for (int j = 0; j < clength; j++)
                            {
                                if (chars[j] == 'f')
                                {
                                    q2 = false;
                                    q3 = false;
                                }
                            }

                            for (int i = 0; i < wlength; i++)
                            {
                                for (int m = (center - kwidth); m <= (center + kwidth); m++)
                                {
                                    Efreq = m.ToString();
                                    if (words[i] == "f" + Efreq)
                                    {
                                        q2 = true;
                                        q3 = true;
                                        q4 = 0;
                                    }
                                }
                            }
//this line is for the debug
                            //if (q1)
                            //{
                            //    richTextBox1.Text += "\r\n" + buffer;
                            //}
                            if (q2 && q1)
                            {
                                //write out the conformer #
                                if (q3)
                                {
                                    richTextBox2.Text += "\r\n" + kmaxS +" "+words[1];
                                    q3 = false;
                                    //q4 = 0;
                                }
                                //writing out the useful information
                                lineout = "";
                                //for (int p = 0; p < wlength; p++)
                                //{
                              //I think "OUT is missing  note I modefied 48.dd"
                                    if (words[3] == "STRE")
                                    {
                                        lineout += " " + words[4] + " " + words[5] + " " + words[6];
                                        q4 = 1;
                                    }
                                    else if (words[3] == "BEND")
                                    {
                                        lineout += " " + words[4] + " " + words[5] + " " + words[6] + " " + words[7];
                                        q4 = 2;
                                    }
                                    else if (words[3] == "TORS" )
                                    {
                                        lineout += " " + words[4] + " " + words[5] + " " + words[6] + " " + words[7] + " " + words[8];
                                        q4 = 3;
                                    }
                                    else if (words[3] == "OUT")
                                    {
                                        lineout += " " + words[4] + " " + words[5] + " " + words[6] + " " + words[7] + " " + words[8];
                                        q4 = 3;
                                    }
                                    else if (q4 == 1)
                                    {
                                        lineout += " " + words[1] + " " + words[2] + " " + words[3];
                                    }
                                    else if (q4 == 2)
                                    {
                                        lineout += " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4];
                                    }
                                    else if (q4 == 3)
                                    {
                                        lineout += " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4] + " " + words[5];
                                    }
                                    //}
                                    richTextBox2.Text += "\r\n" + lineout;
                            }
                        }
                        sr.DiscardBufferedData();
                        sr.Close();
                    }
                    
                }//end of each frequency
                progressBar1.Value = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {

//                string sourceDir=@"C:\\Users\\Hideya\\Desktop\\a-TROCIAcheckpoint\\"; 
            //I found bug it is reading frequencies outside the range chosen  try 1000+-50  you will see 310
            string sourceDir = @""; // @"..\\..";    

                int kmin = Convert.ToInt32(textBox3.Text);
                int kmax = Convert.ToInt32(textBox2.Text);
                int kwidth = Convert.ToInt32(textBox4.Text);
                int center = Convert.ToInt32(textBox1.Text); ;
                int Maxlinenumber;
                    
                    Maxlinenumber = richTextBox2.Lines.Count();
                    progressBar1.Minimum = 0;
                    progressBar1.Maximum = Maxlinenumber;

                    String buffer;
                    String buffer1;
                    string temp1;
                    for (int i = 1; i < Maxlinenumber; i++)
                    {
                        progressBar1.Value = i; 
                        buffer = richTextBox2.Lines[i];
                            string[] words = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            char[] chars = buffer.ToCharArray();

                            if (chars[0] == '0')
                            {
                                using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + words[0] + ".vd"))
                                {
                                    double targFreq = Convert.ToDouble(textBox1.Text);
                                    while (sr.Peek() >= 0)
                                    {
                                        buffer1 = sr.ReadLine();
                                        string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                        temp1 = "s" + words[1];
                                        if (words1.Length > 2)
                                        {
                                           // if (words1[1] == temp1)
                                            if (words1[1] == temp1 && Math.Abs(Convert.ToDouble(words1[0])-targFreq)<Convert.ToDouble(textBox4.Text))
                                            {
                                                richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                    temp1 + " weight% " + words1[2] + "\r\n";
                                            }
                                        }    
                                        if (words1.Length > 4)
                                        {
                                            if (words1[3] == temp1 && Math.Abs(Convert.ToDouble(words1[0]) - targFreq) < Convert.ToDouble(textBox4.Text))
                                            {
                                                richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                    temp1 + " weight% " + words1[4] + "\r\n";
                                            }
                                        }
                                        if (words1.Length > 6)
                                        {
                                            if (words1[5] == temp1 && Math.Abs(Convert.ToDouble(words1[0]) - targFreq) < Convert.ToDouble(textBox4.Text))
                                                {
                                                    richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                        temp1 + " weight% " + words1[6] + "\r\n";
                                                }
                                        }
                                        if (words1.Length > 8)
                                        {
                                            if (words1[5] == temp1 && Math.Abs(Convert.ToDouble(words1[0]) - targFreq) < Convert.ToDouble(textBox4.Text))
                                            {
                                                richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                    temp1 + " weight% " + words1[8] + "\r\n";
                                            }
                                        }

                                        if (words1.Length > 10)
                                        {
                                            if (words1[5] == temp1 && Math.Abs(Convert.ToDouble(words1[0]) - targFreq) < Convert.ToDouble(textBox4.Text))
                                            {
                                                richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                    temp1 + " weight% " + words1[10] + "\r\n";
                                            }
                                        }

                                        if (words1.Length > 12)
                                        {
                                            if (words1[5] == temp1 && Math.Abs(Convert.ToDouble(words1[0]) - targFreq) < Convert.ToDouble(textBox4.Text))
                                            {
                                                richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                    temp1 + " weight% " + words1[12] + "\r\n";
                                            }
                                        }
                                        if (words1.Length > 14)
                                        {
                                            if (words1[5] == temp1 && Math.Abs(Convert.ToDouble(words1[0]) - targFreq) < Convert.ToDouble(textBox4.Text))
                                            {
                                                richTextBox1.Text += "conf# " + words[0] + " CalcFreq " + words1[0] + " LocalMode# " +
                                                    temp1 + " weight% " + words1[14] + "\r\n";
                                            }
                                        }

                                    }

                                    sr.Close();
                                }

                            }
                            else { 
                            richTextBox1.Text+=buffer+ "\r\n";
                            }

                    }

                    richTextBox2.Text = richTextBox1.Text;
                    richTextBox1.Text = "";
                        progressBar1.Value = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //                string sourceDir=@"C:\\Users\\Hideya\\Desktop\\a-TROCIAcheckpoint\\";    
            string sourceDir = @""; // @"..\\..";    

            int Maxlinenumber;
            double TargetFreq;
            double[] freq;
            freq = new double[3];

            double error;
            double v0 = (6.62606957E-34 * 299792458 / (Convert.ToDouble(textBox8.Text) * 0.000000001)) / 1.602176565E-19*8065.54;
            
            bool[] q;
            q = new bool[3];
            Maxlinenumber = richTextBox2.Lines.Count();

            progressBar1.Minimum = 0;
            progressBar1.Maximum = Maxlinenumber;

            String buffer;
            String buffer1;
            double convfac;
            
            //string temp1;
            for (int i = 0; i < Maxlinenumber; i++)
            {
                progressBar1.Value = i;
                buffer = richTextBox2.Lines[i];
                string[] words = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                char[] chars = buffer.ToCharArray();
                if(words.Length>1){
                    if (words[0] == "conf#")
                    {

                        TargetFreq = Convert.ToDouble(words[3]);

                        using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + words[1] + textBox9.Text))
                        {
                            while (sr.Peek() >= 0)
                            {
                                buffer1 = sr.ReadLine();
                                string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                if (words1.Length > 1)
                                {
                                    if (words1[0] == "Frequencies")
                                    {

                                        for (int n = 2; n < words1.Length; n++)
                                        {
                                            freq[n - 2] = Convert.ToDouble(words1[n]);
                                            error = (freq[n - 2] - TargetFreq) * (freq[n - 2] - TargetFreq);
                                            if (error < 0.01) { q[n - 2] = true; }

                                        }

                                    }
                                    if (checkBox1.Checked==true)
                                    {
                                        if (words1[0] == "Raman" && (q[0] || q[1] || q[2]))
                                        {
                                           // richTextBox5.Text = words1[3] + "\r\n";
                                            if (q[0])
                                            {
                                                convfac = Math.Pow((v0 - freq[0]), 4.0) / (freq[0] * (1.0 - Math.Exp((-4.8332E-05 * freq[0])))) / 1.0e14;
                                                richTextBox1.Text += buffer + " Ramanintensity " + Convert.ToString(Convert.ToDouble(words1[3]) * convfac) + "\r\n";
                                                                                                                             
                                                
                                               // richTextBox1.Text += buffer + " Ramanintensity " + words1[3] + "\r\n";
                                                q[0] = false;
                                            }
                                            if (q[1])
                                            {
                                                convfac = Math.Pow((v0 - freq[1]), 4.0) / (freq[1] * (1.0 - Math.Exp((-4.8332E-05 * freq[1])))) / 1.0e14;
                                                richTextBox1.Text += buffer + " Ramanintensity " + Convert.ToString(Convert.ToDouble(words1[4]) * convfac) + "\r\n";
                                                
                                                //richTextBox1.Text += buffer + " Ramanintensity " + words1[4] + "\r\n";
                                                q[1] = false;
                                            }
                                            if (q[2])
                                            {
                                                convfac = Math.Pow((v0 - freq[2]), 4.0) / (freq[2] * (1.0 - Math.Exp((-4.8332E-05 * freq[2])))) / 1.0e14;
                                                richTextBox1.Text += buffer + " Ramanintensity " + Convert.ToString(Convert.ToDouble(words1[5]) * convfac) + "\r\n";

                                                //richTextBox1.Text += buffer + " Ramanintensity " + words1[5] + "\r\n";
                                                q[2] = false;
                                            }
                                        }

                                    }
                                    else
                                    {
                                        if (words1[1] == "Inten" && (q[0] || q[1] || q[2]))
                                        {
                                            if (q[0])
                                            {
                                                richTextBox1.Text += buffer + " IRintensity " + words1[3] + "\r\n";
                                                q[0] = false;
                                            }
                                            if (q[1])
                                            {
                                                richTextBox1.Text += buffer + " IRintensity " + words1[4] + "\r\n";
                                                q[1] = false;
                                            }
                                            if (q[2])
                                            {
                                                richTextBox1.Text += buffer + " IRintensity " + words1[5] + "\r\n";
                                                q[2] = false;
                                            }

                                        }
                                    }
                                }

                                //I get the free energy here
                                //if (words1.Length > 7)
                                //{
                                //    if (words1[5] == "Free" && words1[6] == "Energies=")
                                //    {
                                //        richTextBox1.Text += "Gibb's Free E "+words1[7] + "\r\n";
                                //    }
                                //}
                            }

                            sr.Close();
                        }

                    }
                    else
                    {
                        richTextBox1.Text += buffer + "\r\n";
                    }
            }
            }
            richTextBox2.Text = "";
            //adding the probability of spesific mode
            double  Maxlinenumber1 = richTextBox1.Lines.Count();
            double Maxlinenumber2 = richTextBox3.Lines.Count();
            
            int confnumber;
            double strength;
            for (int j = 0; j < Maxlinenumber1; j++)
            {
                buffer = richTextBox1.Lines[j];
                string[] words = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (words.Length > 1)
                {
                    if (words[0] == "conf#")
                    {
                        confnumber = Convert.ToInt32(words[1]);
                        buffer1 = richTextBox3.Lines[confnumber];
                        string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        strength=Math.Abs(Convert.ToDouble(words[7])) * Convert.ToDouble(words[9]) * Convert.ToDouble(words1[2])/100.0;
                        richTextBox2.Text += buffer + " confpop " + words1[2] + " strength "+ strength.ToString()+"\r\n";
                        
                    }
                    else
                    {
                        richTextBox2.Text += buffer + "\r\n";
                    }
                }                

            }
            richTextBox1.Text = "";
            double Maxlinenumber3 = richTextBox2.Lines.Count();

            for (int j = 0; j < Maxlinenumber1; j++)
            {
                buffer = richTextBox2.Lines[j];
                string[] words = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (words.Length > 1)
                {
                    if (words[0] == "conf#")
                    {
                        strength = Convert.ToDouble(words[13]);
                        richTextBox1.Text += " strength " + strength.ToString() + "\r\n";

                    }
                    else
                    {
                        richTextBox1.Text += buffer + "\r\n";
                    }
                }

            }
            bool q4 = false;
            Maxlinenumber1 = richTextBox1.Lines.Count();
            strength = 0.0;
            for (int j = 0; j < Maxlinenumber1; j++)
            {
                buffer = richTextBox1.Lines[j];
                string[] words = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (words.Length > 1)
                {
                    if (words[0] == "strength"&& q4==false)
                    {
                        strength = 0.0;
                        strength = Convert.ToDouble(words[1]);
                        q4 = true;   
                    }
                    else if (words[0] == "strength" && q4 == true)
                    {
                        strength += Convert.ToDouble(words[1]);
                        q4 = true;

                    }
                    else
                    {
                        richTextBox4.Text += buffer + " strength "+strength.ToString() +"\r\n";
                        q4 = false;
                    }
                }
            
            
            
            }



               // richTextBox2.Text = richTextBox1.Text;


            progressBar1.Value = 0;


        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sourceDir = @""; // @"..\\..";    
                string kmaxS;
                int kmin = 1;
                int kmax = Convert.ToInt32(textBox5.Text);
                
                progressBar1.Minimum = 0;
                progressBar1.Maximum = kmax;

                string buffer1;
                double totalpop=0.0;
                double partialpop;
                double GDState=0.0;
                double EachState;
                double RT = 0.000944202;
                String dipole="";

                double Etherm0, Etherm1, Stherm0, Stherm1,Gtherm0, Gtherm1,Gcorrection, AveGcorr;
                int HRsuccess=0;
                AveGcorr = 0.0;
                bool thermCheck=false;
                for (int i = kmin; i <= kmax; i++)
                {
                    progressBar1.Value = i;
                    kmaxS = i.ToString();
                    if (kmaxS.Length == 1) { kmaxS = "000" + kmaxS; }
                    if (kmaxS.Length == 2) { kmaxS = "00" + kmaxS; }
                    if (kmaxS.Length == 3) { kmaxS = "0" + kmaxS; }

                    Gcorrection = 0;
                    if (checkBox2.Checked == true)//I get all Gcorrection & store them into RTB8  Some value is not available(HR calc failed) so I put 0 there
                    {
                        Etherm0 = 0;
                        Etherm1 = 0;
                        Stherm0 = 0;
                        Stherm1 = 0;
                        Gtherm0 = 0;
                        Gtherm1 = 0;
                        
                        using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + kmaxS + textBox9.Text))
                        {
                            while (sr.Peek() >= 0)
                            {
                                buffer1 = sr.ReadLine();
                                string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


                                if (words1.Length == 4 && words1[0] == "Total" && thermCheck == true)
                                {
                                    Etherm0 = Convert.ToDouble(words1[1])*0.0015936;
                                    Stherm0 = Convert.ToDouble(words1[3])*0.0015936;
                                    Gtherm0 = Etherm0 + 1.38064852e-23 * 298.15 / 4.35981e-18-298.15 * Stherm0 / 1000;
                                }

                                if (words1.Length == 5 && words1[0] == "internal" && words1[1] == "rot." && thermCheck == true)
                                {
                                    Etherm1 = Convert.ToDouble(words1[2]) * 0.0015936;
                                    Stherm1 = Convert.ToDouble(words1[4]) * 0.0015936;
                                    Gtherm1 = Etherm1 + 1.38064852e-23 * 298.15 / 4.35981e-18 - 298.15 * Stherm1 / 1000;
                                    thermCheck = false;
                                }

                                if (words1.Length == 4 && words1[1] == "(Thermal)")
                                {
                                    thermCheck = true;
                                }
                            }


                            sr.Close();

                        }
                        Gcorrection = Gtherm1 - Gtherm0;
                        richTextBox7.Text += Convert.ToString(Gcorrection) + "\r\n";
                        if(Gcorrection !=0.0){
                        AveGcorr+=Gcorrection;
                        HRsuccess++;
                        }
                        //richTextBox7.Text += Convert.ToString(Etherm1) + "\r\n";
                        //richTextBox7.Text += Convert.ToString(Stherm0) + "\r\n";
                        //richTextBox7.Text += Convert.ToString(Stherm1) + "\r\n";
                        //richTextBox7.Text += Convert.ToString(Gtherm0) + "\r\n";
                    }

                    using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + kmaxS + textBox9.Text))//just look for the minimum energy & store it into GDstate
                        {
                            while (sr.Peek() >= 0)
                            {
                                buffer1 = sr.ReadLine();
                                string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                
                                //I get the free energy here
                                if (words1.Length > 7)
                                {
                                    if (words1[5] == "Free" && words1[6] == "Energies=")
                                    {
                                        richTextBox3.Text += "Gibb's Free E "+words1[7] + "\r\n";

                                            if(checkBox2.Checked == true)
                                            {
                                            EachState = Convert.ToDouble(words1[7])+ Gcorrection;
                                            }
                                            else
                                            {
                                                EachState = Convert.ToDouble(words1[7]);
                                            }
                                        if (EachState < GDState)
                                        {
                                          GDState = EachState;
                                        }
                                    }
                                }

                                if (words1.Length > 6)
                                {
                                    if (words1[6] == "Tot=")
                                    {
                                        dipole = words1[7];
                                    }
                                }
                            }


                            sr.Close();
                        }

                    richTextBox6.Text += dipole + "\r\n";
                }
                AveGcorr = AveGcorr / HRsuccess;

                richTextBox8.Text = "Average of hindered Rotor correction "+ Convert.ToString(AveGcorr)+"\r\n";

                    double tempvalue=0.0;   
                    richTextBox3.Text = "";
                    richTextBox3.Text += GDState.ToString() + "\r\n";
                    
                    for (int i = kmin; i <= kmax; i++)
                    {
                        progressBar1.Value = i;
                        kmaxS = i.ToString();
                        if (kmaxS.Length == 1) { kmaxS = "000" + kmaxS; }
                        if (kmaxS.Length == 2) { kmaxS = "00" + kmaxS; }
                        if (kmaxS.Length == 3) { kmaxS = "0" + kmaxS; }

                        using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + kmaxS + textBox9.Text))
                        {
                            while (sr.Peek() >= 0)
                            {
                                buffer1 = sr.ReadLine();
                                string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                                //I get the free energy here
                                if (words1.Length > 7)
                                {
                                    if (words1[5] == "Free" && words1[6] == "Energies=")
                                    {
                                        //richTextBox3.Text += "Gibb's Free E " + words1[7] + "\r\n";
                                        if (checkBox2.Checked == true)
                                        {
                                            if (richTextBox7.Lines[i - 1] != "0")
                                            {
                                                tempvalue = Convert.ToDouble(richTextBox7.Lines[i - 1]);
                                            }
                                            else
                                            {
                                                tempvalue = AveGcorr;
                                            }
                                            
                                            EachState = Math.Exp(-(Convert.ToDouble(words1[7]) + tempvalue - GDState) / RT);
                                            totalpop += EachState;
                                            richTextBox8.Text +=  tempvalue + "\r\n";
                                        }
                                        else
                                        {
                                            EachState = Math.Exp(-(Convert.ToDouble(words1[7]) - GDState) / RT);
                                            totalpop += EachState;
                                        }

                                    }
                                }
                            }

                            sr.Close();
                        }
                    }

                    for (int i = kmin; i <= kmax; i++)
                    {
                        progressBar1.Value = i;
                        kmaxS = i.ToString();
                        if (kmaxS.Length == 1) { kmaxS = "000" + kmaxS; }
                        if (kmaxS.Length == 2) { kmaxS = "00" + kmaxS; }
                        if (kmaxS.Length == 3) { kmaxS = "0" + kmaxS; }


                        using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + kmaxS + textBox9.Text))
                        {
                            while (sr.Peek() >= 0)
                            {
                                buffer1 = sr.ReadLine();
                                string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                                //I get the free energy here
                                if (words1.Length > 7)
                                {
                                    if (words1[5] == "Free" && words1[6] == "Energies=")
                                    {

                                        if (checkBox2.Checked == true)
                                        {
                                            if (richTextBox7.Lines[i - 1] != "0")
                                            {
                                                tempvalue = Convert.ToDouble(richTextBox7.Lines[i - 1]);
                                            }
                                            else
                                            {
                                                tempvalue = AveGcorr;
                                            }
                                            partialpop = Math.Exp(-(Convert.ToDouble(words1[7]) + tempvalue - GDState) / RT) / totalpop;
                                            richTextBox3.Text += kmaxS + " population " + partialpop.ToString() + "\r\n";
                                            richTextBox10.Text += partialpop.ToString() + "\r\n";
                                        }
                                        else
                                        {
                                            partialpop = Math.Exp(-(Convert.ToDouble(words1[7]) - GDState) / RT) / totalpop;
                                            richTextBox3.Text += kmaxS + " population " + partialpop.ToString() + "\r\n";
                                            richTextBox10.Text += partialpop.ToString() + "\r\n";
                                        }

                                    }
                                }
                            }

                            sr.Close();
                        }

                    }
            //richTextBox2.Text = richTextBox3.Text;
            //richTextBox3.Text = "";
           // richTextBox1.Text += richTextBox3.GetLineFromCharIndex(21);
                    
            progressBar1.Value = 0;

        }

        private void button5_Click(object sender, EventArgs e)
        {


            int length1;
            bool q1=false;
            double strength=0.0;
            string buffer,buffer1,buffer2;
            int maxlinenumber = richTextBox4.Lines.Count();
            int maxlinenumber1 = richTextBox5.Lines.Count();
            int ws = 0;

            for (int s = 0; s < maxlinenumber-1; s++)
            {
                //while (richTextBox4.Lines[s].ToString() == "")
                //{
                //    s++;
                //}
                q1 = false;
                buffer1 = richTextBox4.Lines[s];
                char[] chars1 = buffer1.ToCharArray();
                length1 = chars1.Length;
                string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (length1 > 3)
                {
                    //store the 1st line to 5th box
                    for (int l = 0; l < words1.Length - 1; l++)
                    {
                        richTextBox5.Text += words1[l] + " ";
                    }
                    //delete the first non empty line
                    richTextBox4.Select(richTextBox4.GetFirstCharIndexFromLine(s), length1);
                    richTextBox4.Text = richTextBox4.Text.Replace(richTextBox4.SelectedText, string.Empty);

                    strength = 0.0;
                    strength += Convert.ToDouble(words1[words1.Length - 1]);
                    q1 = true;
                    //ws++;//increase the counter for box5
                }

                if (q1)
                {
                    for (int i = 0; i < maxlinenumber - 1; i++)
                    {//loop over all line in box 4
                        //get the current line from 5th box 
                        //buffer1 = richTextBox5.Lines[0];
                        //string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        //get the current line from box 4
                        buffer = richTextBox4.Lines[i];
                        string[] words = buffer.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        char[] chars = buffer.ToCharArray();
                        length1 = chars.Length;

                        if (words.Length > 3 && words1.Length > 3)
                        {
                            if (words1[0] == words[0] && words1[1] == words[1] && words1[2] == words[2] && words1[3] == words[3])
                            {
                                //                            richTextBox5.Text += richTextBox4.Lines[i] + "\r\n";
                                strength += Convert.ToDouble(words[words.Length - 1]);

                                richTextBox4.Select(richTextBox4.GetFirstCharIndexFromLine(i), length1);
                                richTextBox4.Text = richTextBox4.Text.Replace(richTextBox4.SelectedText, string.Empty);
                            }
                        }
                    }
                    richTextBox5.Text += strength.ToString() + "\r\n";
                }

            }
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string sourceDir = @""; // @"..\\..";    
            string kmaxS;
            int kmin = 1;
            int kmax = Convert.ToInt32(textBox5.Text);

            progressBar1.Minimum = 0;
            progressBar1.Maximum = kmax;
            double[] ploty= new double [5000];
            double[] tdouble = new double[5000];
            for (int j = 0; j < 5000; j++)
            {
                tdouble[j] = Convert.ToDouble(j);
            }
            for (int j = 0; j < 5000; j++)
            {
                ploty[j]=0.0;
            }

            string buffer1;
            double GFcenter, GFstdev, GFnorm, GFexp, Confpop, GFintensity, convfac;
            double v0 = (6.62606957E-34 * 299792458 / (Convert.ToDouble(textBox8.Text) * 0.000000001)) / 1.602176565E-19 * 8065.54;

            GFstdev=Convert.ToDouble(textBox7.Text);            
            GFnorm=1.0/GFstdev/2.506628275;

            for (int i = kmin; i <= kmax; i++)
            {
                progressBar1.Value = i;
                kmaxS = i.ToString();
                if (kmaxS.Length == 1) { kmaxS = "000" + kmaxS; }
                if (kmaxS.Length == 2) { kmaxS = "00" + kmaxS; }
                if (kmaxS.Length == 3) { kmaxS = "0" + kmaxS; }


                using (StreamReader sr = new StreamReader(sourceDir + textBox6.Text + kmaxS + textBox9.Text))
                {
                    while (sr.Peek() >= 0)
                    {
                        buffer1 = sr.ReadLine();
                        string[] words1 = buffer1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (words1.Length > 3)
                        {
                            if (words1[0] == "Frequencies" && words1[1] == "--")
                            {
                                for (int s = 2; s < words1.Length; s++)
                                {
                                    richTextBox5.Text += words1[s] + "\r\n";
                                }
                            }

                            if (checkBox1.Checked == true)
                            {
                                if (words1[0] == "Raman" && words1[1] == "Activ" && words1[2] == "--")
                                {
                                    for (int s = 3; s < words1.Length; s++)
                                    {
                                        richTextBox4.Text += words1[s] + "\r\n";
                                    }
                                }

                            }
                            else
                            {
                                if (words1[0] == "IR" && words1[1] == "Inten" && words1[2] == "--")
                                {
                                    for (int s = 3; s < words1.Length; s++)
                                    {
                                        richTextBox4.Text += words1[s] + "\r\n";
                                    }
                                }

                            }

                        }
                       
                    }
                    sr.Close();
                }


                    int maxlinenumber = richTextBox4.Lines.Count();
                    Confpop = Convert.ToDouble(richTextBox10.Lines[i-1]);

                if(checkBox1.Checked==true)
                {
                    for (int r = 0; r < maxlinenumber - 1; r++)
                    {
                        GFcenter = Convert.ToDouble(richTextBox5.Lines[r]);
                        GFintensity = Convert.ToDouble(richTextBox4.Lines[r]);
                        convfac = Math.Pow((v0 - GFcenter), 4.0) / (GFcenter * (1.0 - Math.Exp((-4.8332E-05 * GFcenter)))) / 1.0e14;
                        for (int t = 0; t < 5000; t++)
                        {
                            GFexp = -(tdouble[t] - GFcenter) * (tdouble[t] - GFcenter) / (2.0 * GFstdev * GFstdev);
                            ploty[t] += Confpop * GFintensity * GFnorm * Math.Exp(GFexp);
                        }
                    }

                }
                else{

                    for (int r = 0; r < maxlinenumber - 1; r++)
                    {
                        GFcenter = Convert.ToDouble(richTextBox5.Lines[r]);
                        GFintensity = Convert.ToDouble(richTextBox4.Lines[r]);

                        for (int t = 0; t < 5000; t++)
                        {
                            GFexp = -(tdouble[t] - GFcenter) * (tdouble[t] - GFcenter) / (2.0 * GFstdev * GFstdev);
                            ploty[t] += Confpop*GFintensity * GFnorm * Math.Exp(GFexp);
                        }
                    }
                    
                }

                richTextBox4.Text = "";
                richTextBox5.Text = "";

            }

            //plot out the result
            for (int t = 0; t < 5000; t++)
            {
                richTextBox9.Text += Convert.ToString(ploty[t]) + "\r\n";
            }

            progressBar1.Value = 0;
        }


        }







    }

